<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="com.eamoi.ajax.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>message--案例：保存更新数据</title>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../function/ajax_func.js"></script>
<script language="javascript">
/**页面初始化，载入留言**/
function loadMessageList() {
	send_request("GET","../message/messageAction?action=viewMessage",null,"XML",populateMessage);	
}
/**添加留言**/
function addMessage() {
	var f = document.forms[0];
	var str = "";
	str = "messagetitle="+f.messagetitle.value;
	str += "&author="+f.author.value;
	str += "&qq="+f.qq.value;
	str += "&msn="+f.msn.value;
	str += "&yahoo="+f.yahoo.value;
	str += "&email="+f.email.value;
	str += "&content="+f.content.value;
	send_request("POST","../message/messageAction?action=addMessage",str,"XML",populateMessage);
}
/**删除留言**/
function deleteMessage(id) {
	send_request("GET","../message/messageAction?action=deleteMessage&id="+id,null,"XML",populateMessage);	
}
/**更新留言**/
function updateMessage() {
	var f = document.forms[0];
	var str = "";
	str += "id="+f.id.value;
	str += "&messagetitle="+f.messagetitle.value;
	str += "&author="+f.author.value;
	str += "&qq="+f.qq.value;
	str += "&msn="+f.msn.value;
	str += "&yahoo="+f.yahoo.value;
	str += "&email="+f.email.value;
	str += "&content="+f.content.value;
	//window.alert(str);
	send_request("POST","../message/messageAction?action=updateMessage",str,"XML",populateMessage);
}
/**显示留言列表**/
function populateMessage() {
	if (http_request.readyState == 4) { // 判断对象状态
		if (http_request.status == 200) { // 信息已经成功返回，开始处理信息
			var doc = http_request.responseXML;
			var messageList = doc.getElementsByTagName("message");
			if((messageList!=null)&&(messageList.length!=0)) {
				var html = "";
				for(var i=0;i<messageList.length;i++) {
					var id = messageList[i].getAttribute("id");
					var title = (messageList[i].childNodes[0].firstChild==null)?"":unescape(messageList[i].childNodes[0].firstChild.data);
					var author = (messageList[i].childNodes[1].firstChild==null)?"":messageList[i].childNodes[1].firstChild.data;
					var submittime = (messageList[i].childNodes[2].firstChild==null)?"":messageList[i].childNodes[2].firstChild.data;
					var qq = (messageList[i].childNodes[3].firstChild==null)?"":messageList[i].childNodes[3].firstChild.data;
					var msn = (messageList[i].childNodes[4].firstChild==null)?"":messageList[i].childNodes[4].firstChild.data;
					var yahoo = (messageList[i].childNodes[5].firstChild==null)?"":messageList[i].childNodes[5].firstChild.data;
					var email = (messageList[i].childNodes[6].firstChild==null)?"":messageList[i].childNodes[6].firstChild.data;
					var content = (messageList[i].childNodes[7].firstChild==null)?"&nbsp;":messageList[i].childNodes[7].firstChild.data;
					html += "<table width=\"600\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#CCCCCC\">\n";
					html += "<tr bgcolor=\"#FFFFFF\"><td colspan=\"3\">标题："+title+"</td><td align=\"center\"><a href=\"javascript:void(0)\" onClick=\"deleteMessage('"+id+"')\">删除</a>";
					html += " | <a href=\"javascript:void(0)\" onClick=\"getSingleMessage('"+id+"')\">修改</a></td></tr>";
					html += "<tr bgcolor=\"#FFFFFF\"><td width=\"50%\" colspan=\"2\">姓名："+author+"</td><td width=\"50%\" colspan=\"2\">留言时间："+submittime+"</td></tr>";
					html += "<tr bgcolor=\"#FFFFFF\"><td width=\"25%\">QQ："+qq+"</td><td width=\"25%\">MSN："+msn+"</td><td width=\"25%\">YAHOO："+yahoo+"</td><td width=\"25%\">Email："+email+"</td></tr>";
					html += "<tr bgcolor=\"#FFFFFF\"><td colspan=\"4\">"+content+"</td></tr>";
					html += "</table>\n<br>\n";
				}
				document.getElementById("messageList").innerHTML = "";
				document.getElementById("messageList").innerHTML = html;
				initialForm();
				initialSearchForm();
			}
			else {
				document.getElementById("messageList").innerHTML = "";
				initialForm();
				initialSearchForm();
			}
		} else { //页面不正常
			alert("您所请求的页面有异常。");
		}
	}
}
function getSingleMessage(id) {
	send_request("GET","../message/messageAction?action=viewSingleMessage&id="+id,null,"XML",populateForm);	
}
function searchMessage() {
	var f = document.searchForm;
	var str = "";
	str = "criteria_title="+f.criteria_title.value;
	str += "&criteria_author="+f.criteria_author.value;
	str += "&beginSubmittime="+f.beginSubmittime.value;
	str += "&endSubmittime="+f.endSubmittime.value;
	str += "&content="+f.criteria_content.value;
	send_request("POST","../message/messageAction?action=findMessage",str,"XML",populateMessage);
}
/**填充表单**/
function populateForm() {
	if (http_request.readyState == 4) { // 判断对象状态
		if (http_request.status == 200) { // 信息已经成功返回，开始处理信息
			var doc = http_request.responseXML;
			var messageList = doc.getElementsByTagName("message");
			if(messageList[0]!=null) {
				var f = document.forms[0];
				var id = messageList[0].getAttribute("id");
				var title = (messageList[0].childNodes[0].firstChild==null)?"":messageList[0].childNodes[0].firstChild.data;
				var author = (messageList[0].childNodes[1].firstChild==null)?"":messageList[0].childNodes[1].firstChild.data;
				var qq = (messageList[0].childNodes[3].firstChild==null)?"":messageList[0].childNodes[3].firstChild.data;
				var msn = (messageList[0].childNodes[4].firstChild==null)?"":messageList[0].childNodes[4].firstChild.data;
				var yahoo = (messageList[0].childNodes[5].firstChild==null)?"":messageList[0].childNodes[5].firstChild.data;
				var email = (messageList[0].childNodes[6].firstChild==null)?"":messageList[0].childNodes[6].firstChild.data;
				var content= (messageList[0].childNodes[7].firstChild==null)?"":messageList[0].childNodes[7].firstChild.data;
				f.id.value = id;
				f.messagetitle.value = title;
				f.author.value = author;
				f.qq.value = qq;
				f.msn.value = msn;
				f.yahoo.value = yahoo;
				f.email.value = email;
				f.content.value = content;
				document.getElementById("submitPanel").innerHTML = "<input type=\"button\" name=\"Submit\" value=\" 修改 \" class=\"button\" onClick=\"updateMessage()\">&nbsp;";
			}
		} else { //页面不正常
			alert("您所请求的页面有异常。");
		}
	}
}
/**初始化留言表单**/
function initialForm() {
	var f = document.forms[0];
	f.reset();
	document.getElementById("submitPanel").innerHTML = "<input type=\"button\" name=\"Submit\" value=\" 留言 \" class=\"button\" onClick=\"addMessage()\">&nbsp;";
}
/**初始化搜索表单**/
function initialSearchForm() {
	var f = document.searchForm;
	f.reset();
}
</script>

</head>

<body onLoad="loadMessageList()">
<center>
		<span id="messageList"></span>
    <form name="form1" method="post" action="">
			<input type="hidden" name="id" value="">
        <table width="600" border="0" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
            <tr bgcolor="#FFFFFF">
                <td width="80">标题：</td>
                <td colspan="3"><input name="messagetitle" type="text" class="input" id="messagetitle" size="70"></td>
            </tr>
            <tr bgcolor="#FFFFFF">
                <td width="80">姓名：</td>
                <td width="220" colspan="3"><input name="author" type="text" class="input" id="author" size="70"></td>
            </tr>
            <tr bgcolor="#FFFFFF">
                <td colspan="4">QQ：
                    <input name="qq" type="text" class="input" id="qq" size="15">
                MSN：
                <input name="msn" type="text" class="input" id="msn" size="15">
                Yahoo：
                <input name="yahoo" type="text" class="input" id="yahoo" size="15">
                Email：
                <input name="email" type="text" class="input" id="email" size="15"></td>
            </tr>
            <tr bgcolor="#FFFFFF">
                <td width="80" valign="top">留言内容：</td>
                <td colspan="3"><textarea cols="80" name="content" rows="8" class="input_textarea"></textarea></td>
            </tr>
            <tr align="center" bgcolor="#FFFFFF">
              <td colspan="4">
								<span id="submitPanel">
								<input name="Submit" id="Submit" type="button" class="button" value=" 留言 " onClick="addMessage()">
								</span>
								<input name="Reset" type="reset" class="button" value=" 重置 ">
              </td>
            </tr>
        </table>
    </form>
    <form name="searchForm" method="post" action="">
        <table width="600" border="0" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
            <tr bgcolor="#FFFFFF">
                <td width="80">标题：</td>
                <td width="220"><input name="criteria_title" type="text" class="input" id="criteria_title" size="30"></td>
                <td width="80">姓名：</td>
                <td width="220"><input name="criteria_author" type="text" class="input" id="criteria_author" size="30"></td>
            </tr>
            <tr bgcolor="#FFFFFF">
                <td width="80">起始日期：</td>
                <td width="220"><input name="beginSubmittime" type="text" class="input" id="beginSubmittime" size="30"></td>
                <td width="80">截止日期：</td>
                <td width="220"><input name="endSubmittime" type="text" class="input" id="endSubmittime" size="30"></td>
            </tr>
            <tr bgcolor="#FFFFFF">
                <td width="80">内容：</td>
                <td colspan="3"><input name="criteria_content" type="text" class="input" id="criteria_content" size="70"></td>
            </tr>
            <tr align="center" bgcolor="#FFFFFF">
                <td colspan="4">
                    <input name="Search" id="Search" type="button" class="button" value=" 搜索 " onClick="searchMessage()">
                    <input name="Reset" type="reset" class="button" value=" 重置 ">
								</td>
            </tr>
        </table>
    </form>
</center>
</body>
</html>
